package com.im.foodorderingsystem.task;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast;

import com.im.foodorderingsystem.Home;
import com.im.foodorderingsystem.Login;
import com.im.foodorderingsystem.Utility;

public class LoginTask extends AsyncTask<String, String, String> {

	
	SharedPreferences fosSettings;
	Login activity;
	Context context;
	
	
	public LoginTask(Context context){
		this.activity =(Login)context;
		this.context=context;
		
		fosSettings=context.getSharedPreferences("fosSettings", Context.MODE_PRIVATE);
		
	}
	
	@Override
	protected String doInBackground(String... parameters) {
		System.out.println(parameters[0] + " " + parameters[1]);

		
		
		// http://10.0.3.2 for genymortion
		// http://10.0.2.2 for normal emulater

		// String URL =
		// "http://10.0.2.2/androidtest/index.php?r=contact/createm";
		String URL = Utility.loginUrl+
				"&email="+ parameters[0] 
				+ "&password=" + parameters[1];
		// String URL =
		// Utility.host+"/foodorderingsystem/index.php?r=api/user/login&email="+parameters[0]+"&password="+parameters[1];
/*
		DefaultHttpClient httpclient = new DefaultHttpClient(
				new BasicHttpParams());
		HttpGet httppost = new HttpGet(URL);
		httppost.setHeader("Content-type", "application/json");
		InputStream inputStream = null;
		String result = null;

		try {
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			inputStream = entity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					inputStream, "UTF-8"), 8);
			StringBuilder theStringBuilder = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				theStringBuilder.append(line + "\n");
			}
			result = theStringBuilder.toString();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null)
					inputStream.close();
			} catch (Exception e) {
			}
		}
*/
		
		String result = null;
		HttpGetRequest httpgetrequest=new HttpGetRequest(URL);
		result=httpgetrequest.send();
		
		return result;
	}

	@Override
	protected void onPostExecute(String result) {

		System.out.println(result);

		try {
			String response;
			String user_id;
			String email;
			String token;
			
			System.out.println("SP created");
			JSONObject reader = new JSONObject(result);
			response = reader.getString("response");
			
			if (response.equals("true")) {
				token = reader.getString("token");

				JSONObject sys = reader.getJSONObject("user");
				user_id = sys.getString("user_id");
				email = sys.getString("email");

				 System.out.println("user_id="+user_id+" email="+email);

				 System.out.println("2");
				 
				Editor editor = fosSettings.edit();
				editor.putString("user_id", user_id);
				editor.putString("email", email);
				editor.putString("token", token);
				editor.commit();
				
				System.out.println("start intent");
				Intent intent = new Intent(activity, Home.class);
				
				activity.startActivity(intent);
				
			} else {
				Toast.makeText(context,
						"Incorect username or password", Toast.LENGTH_LONG)
						.show();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		// super.onPostExecute(result);
	}

}
